
# 观察子过程调用。调用需要在call时发动一个可能产生反馈的动作，这里是观察。

def main():
    lib = Lib()
    # 当在【静息】状态接收到【刺激0】刺激，发出【观察】动作，呼叫【观察状态】过程
    lib.reflect("刺激0", "静息", "call", "观察状态", "观察")
    # 当在【观察状态】状态接收到【特征1】刺激，跳转【观察状态2】状态，发出【观察】动作
    lib.reflect("特征1", "观察状态", "观察状态2", "观察")
    # 当在【观察状态2】状态接收到【特征2】刺激，发出【寄存：苹果】动作，结束当前过程并回到上级过程
    lib.reflect("特征2", "观察状态2", "ret", "pack:苹果")
    # 当在【观察状态2】状态接收到【特征4】刺激，发出【寄存：梨】动作，结束当前过程并回到上级过程
    lib.reflect("特征4", "观察状态2", "ret", "pack:梨")

    # 当在【静息】状态接收到【完成】刺激，发出【回忆】动作
    lib.reflect("完成", "静息", "trace:")
    # 当在【静息】状态接收到【苹果】刺激，发出【说苹果】动作
    lib.reflect("苹果", "静息", "说苹果")
    # 当在【静息】状态接收到【梨】刺激，发出【说梨】动作
    lib.reflect("梨", "静息", "说梨")


    test(lib, A(), time =5)



from ai import Lib, Stater, test, Timer, Passer, EnvMesEvent
from random import random

lib = Lib()

class A(Stater):
    def __init__(self):
        self.n = 0
        if random() < 0.5:
            self.n = 5


    def reg_event(self, frame):
        frame.act_callback.append(self.reply)

        p = Passer(1)
        @p.bindend
        def f():
            # ~ print("咚")
            frame.post_event(EnvMesEvent("刺激0"))
        p.reg_event(frame)

    def reply(self, frame, e):
        a = e.tag
        if a == "观察" and self.n == 0:
            r = "特征1"
            self.n += 1
        elif a == "观察" and self.n == 1:
            r = "特征2"
            self.n += 1
        elif a == "观察" and self.n == 5:
            r = "特征1"
            self.n += 1
        elif a == "观察" and self.n == 6:
            r = "特征4"
            self.n += 1
        else:
            return

        p = Passer(1)
        @p.bindend
        def f():
            frame.post_event(EnvMesEvent(r))
        p.reg_event(frame)


main()
